package com.haojii.notifier.user;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class UserServlet extends HttpServlet {
	
	
/*	private static final String USER_MODIFY_JSP = "user_modify.jsp";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		dispatchUser(request, response); 
	}

	private void dispatchUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService userService = UserServiceFactory.getUserService(); 
		 
        String thisURL = request.getRequestURI(); 
        if (request.getUserPrincipal() != null) { 
        	com.google.appengine.api.users.User u = userService.getCurrentUser();
        	User user = UserUtils.get(u.getEmail());
        	if(user==null)
        	{
        		user = new User(u, "");
        	}
        	request.setAttribute("user", user);
        	
        	request.setAttribute("LogoutURL", userService.createLogoutURL(thisURL));
        	
        } else { 
        	request.setAttribute("LoginURL", userService.createLoginURL(thisURL));
        }
        request.getRequestDispatcher(USER_MODIFY_JSP).forward(request, response);
	}*/

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//notifierType=email&userCellphone=13764122435
		String[] notifierTypes = request.getParameterValues("notifierType");
		String cellphone = request.getParameter("cellphone");
		//String email = req.getParameter("email");
		UserService userService = UserServiceFactory.getUserService(); 
		User user = new User(userService.getCurrentUser(), cellphone);
		
		String notifierType = "";
		if(notifierTypes != null)
		{
			for(String s:notifierTypes)
			{
				notifierType+= s;
				notifierType+= ",";
			}
		}
		user.setNotifierType(notifierType);
		UserUtils.save(user);
		
		request.setAttribute("user", user);
		
		response.sendRedirect("/easytv");
//		request.getRequestDispatcher("/easytv").forward(request, response);
	}
	
	
}
